import { request } from '@/service/request';

/**
 * 数据服务API接口
 */

/** 数据API */
export interface DataApi {
  id?: number;
  name: string;
  path: string;
  method: string;
  sqlTemplate: string;
  requestParams?: string;
  responseFields?: string;
  authType?: string;
  status?: string;
  version?: string;
  rateLimit?: number;
  desensitizeRules?: string;
  description?: string;
  projectId?: number;
  createTime?: string;
  updateTime?: string;
  remark?: string;
}

/** API响应 */
export interface ApiResponse {
  code: number;
  message: string;
  data: any[];
  total: number;
  responseTime: number;
}

/** API统计 */
export interface ApiStatistics {
  totalCalls: number;
  successCalls: number;
  failedCalls: number;
  successRate: number;
  avgResponseTime: number;
  maxResponseTime: number;
  minResponseTime: number;
  todayCalls: number;
  weekCalls: number;
  monthCalls: number;
}

/** API调用日志 */
export interface ApiCallLog {
  id: number;
  apiId: number;
  apiName: string;
  apiPath: string;
  caller: string;
  callerIp: string;
  requestParams: string;
  responseData: string;
  statusCode: number;
  responseTime: number;
  errorMessage?: string;
  callTime: string;
}

/**
 * 获取数据API列表
 */
export function fetchDataApiList(params?: any) {
  return request<ApiRes.PageData<DataApi>>({
    url: '/dataplatform/data-api/list',
    method: 'get',
    params
  });
}

/**
 * 获取数据API详情
 */
export function fetchDataApiDetail(id: number) {
  return request<ApiRes.Data<DataApi>>({
    url: `/dataplatform/data-api/${id}`,
    method: 'get'
  });
}

/**
 * 新增数据API
 */
export function addDataApi(data: DataApi) {
  return request<ApiRes.Data<number>>({
    url: '/dataplatform/data-api',
    method: 'post',
    data
  });
}

/**
 * 修改数据API
 */
export function updateDataApi(data: DataApi) {
  return request<ApiRes.Data<void>>({
    url: '/dataplatform/data-api',
    method: 'put',
    data
  });
}

/**
 * 删除数据API
 */
export function deleteDataApi(ids: number[]) {
  return request<ApiRes.Data<void>>({
    url: `/dataplatform/data-api/${ids.join(',')}`,
    method: 'delete'
  });
}

/**
 * 发布API
 */
export function publishApi(id: number) {
  return request<ApiRes.Data<void>>({
    url: `/dataplatform/data-api/${id}/publish`,
    method: 'post'
  });
}

/**
 * 下线API
 */
export function offlineApi(id: number) {
  return request<ApiRes.Data<void>>({
    url: `/dataplatform/data-api/${id}/offline`,
    method: 'post'
  });
}

/**
 * 测试API
 */
export function testApi(id: number, params: any) {
  return request<ApiRes.Data<ApiResponse>>({
    url: `/dataplatform/data-api/${id}/test`,
    method: 'post',
    data: params
  });
}

/**
 * 获取API统计信息
 */
export function fetchApiStatistics(id: number, startDate: string, endDate: string) {
  return request<ApiRes.Data<ApiStatistics>>({
    url: `/dataplatform/data-api/${id}/statistics`,
    method: 'get',
    params: { startDate, endDate }
  });
}

/**
 * 获取API调用日志
 */
export function fetchApiCallLogs(id: number, params?: any) {
  return request<ApiRes.PageData<ApiCallLog>>({
    url: `/dataplatform/data-api/${id}/logs`,
    method: 'get',
    params
  });
}
